
//
// 130728 - AH - (Created).
//

package org.japril.jutil.pulse;

import java.util.ArrayList;
import java.util.List;

import org.japril.jutil.logging.Log;

public final class Processors
{
  private final List<Processor> m_processors=new ArrayList<Processor>();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public Processors(){}
  
  /**
   * <p>
   *   Adds 
   *   {@link Processor}.
   * </p>
   * @param p {@code Processor} to add.
   */
  public void addProcessor(Processor p)
  {
	if (p==null)
	{
	  throw new NullPointerException("p");
	}
	
	m_processors.add(p);
  }
  
  /**
   * <p>
   *   Remove
   *   {@link Processor}.
   * </p>
   * @param p {@link Processor}.
   * @return {@code true} if removed else {@code false} if not removed.
   */
  public boolean removeProcessor(Processor p)
  {
    if (p==null)
    {
      throw new NullPointerException("p");
    }
		
	return m_processors.remove(p);
  }
  
  /**
   * <p>
   *   Gets index of 
   *   {@link Processor}.
   * </p>
   * @param p {@link Processor}.
   * @return Index or {@code -1} if not found.
   */
  public int indexOf(Processor p)
  {
    if (p==null)
	{
	  throw new NullPointerException("p");
	}
    
    return m_processors.indexOf(p);
  }
  
  /**
   * <p>
   *   Removes all
   *   {@link Processor}s.
   * </p>
   */
  public void clear(){ m_processors.clear(); }
  
  /**
   * <p>
   *   Gets number of
   *   {@link Processor}.
   * </p>
   * @return {@link Processor}.
   */
  public int size(){ return m_processors.size(); }
  
  /**
   * <p>
   *   Gets
   *   {@link Processor}.
   * </p>
   * @param idx Index of
   *            {@link Processor} to get.
   * @return {@link Processor}.
   * @throws IndexOutOfBoundsException If {@code idx<0 || idx>=size()}.
   */
  public Processor getProcessor(int idx){ return m_processors.get(idx); }
  
  /**
   * <p>
   *   Process.
   * </p>
   * @param products {@link Products}.
   */
  public void process(Products products)
  {
    if (products==null)
    {
      throw new NullPointerException("products");
    }
    
    Log.log(c_log, this, "process", "pulse: "+products.getPulseName());
    
    List<Processor> l=m_processors;
    for (Processor processor : l) processor.process(products);
  }
  
  private static boolean c_log=true;
  
}
